package com.linkoog.devtools.metadata.raw_index;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiJavaFile;
import com.linkoog.devtools.metadata.DocumentMetadata;
import com.linkoog.devtools.raw_index.PsiRawFileIndex;
import com.linkoog.devtools.type.javadoc.JavaDoc;
import com.linkoog.devtools.type.javadoc.JavaDocParser;
import com.linkoog.devtools.type.swagger.JavaSwaggerParser;
import com.linkoog.devtools.utils.PsiClassUtils;
import com.linkoog.devtools.utils.StringUtils;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class ClassMetadata extends DocumentMetadata<PsiClass> implements PsiRawFileIndex.Item<PsiClass> {

    @Getter
    protected String moduleFilePath;
    @Getter @JsonIgnore
    protected String packageName;
    @Getter @JsonIgnore
    protected String shortName;
    @JsonIgnore
    protected PsiClass psiClass;


    @JsonCreator
    public ClassMetadata(@JsonProperty("filePath")@NotNull String filePath,
                         @JsonProperty("psiElementOffset") int psiElementOffset,
                         @JsonProperty("name") String name,
                         @JsonProperty("moduleFilePath") String moduleFilePath
    ) {
        super(filePath, psiElementOffset, name);
        this.moduleFilePath = moduleFilePath;
    }

    @Override
    public boolean initPsiElement(@NotNull Project project, @Nullable PsiClass psiClass){
        if (this.psiClass != null) return true;

        //在创建的时候初始化
        if (psiClass != null){
            this.psiClass = psiClass;
            this.packageName = PsiClassUtils.getPackageName(psiClass);
            this.shortName = psiClass.getName();
            return true;
        }

        //在项目启动后初始化
        PsiClass psi = PsiClassUtils.findPsiClass(project, filePath, psiElementOffset);
        if (psi != null){
            this.psiClass = psi;
            this.packageName = PsiClassUtils.getPackageName(psi);
            this.shortName = psi.getName();
            return true;
        }
        return false;
    }

    @Override @JsonIgnore @NotNull
    public PsiClass getPsiElement() {
        return psiClass;
    }

    public JavaDoc getJavaDoc() {
        if(javaDoc == null) {
            synchronized (this) {
                if(javaDoc == null) {
                    if(javaDocSupplier != null) {
                        javaDoc = javaDocSupplier.get();
                    } else {
                        javaDoc = JavaDocParser.parse(psiClass.getDocComment());
                    }
                }
            }
        }
        return javaDoc;
    }

    public String getSwaggerComment() {
        if(swaggerComment == null) {
            synchronized (this) {
                if(swaggerComment == null) {
                    if(swaggerCommentSupplier != null) {
                        swaggerComment = swaggerCommentSupplier.get();
                    } else {
                        swaggerComment = JavaSwaggerParser.parseClass(psiClass);
                    }
                }
            }
        }
        return swaggerComment;
    }
}
